package com.hdy.web.controller;

import com.hdy.web.dao.UserMapper;
import com.hdy.web.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;

@Controller
public class LoginController {
    final UserMapper userMapper;
    public LoginController(UserMapper userMapper) {
        this.userMapper = userMapper;
    }
    @RequestMapping("/user/login")
    public String login(
            @RequestParam("userName") String userName,
            @RequestParam("password") String password,
            Model model,
            HttpSession session
    ){
        User user = userMapper.getUser(userName, password);
        if(user!=null){
            session.setAttribute("userName",userName);
            return "redirect:/main";
        }else {
            model.addAttribute("msg","用户名或密码错误！");
            return "index";
        }
    }
    @RequestMapping("/user/signout")
    public String logout(HttpSession session){
        session.removeAttribute("userName");
        return "redirect:signin";
    }

}
